﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuickDrawScreen
{
    public partial class _DrawForm : Form
    {

        /// <summary>
        /// 是否开始画图
        /// </summary>
        private Boolean startDrawing = false;

        /// <summary>
        /// 画图起始位置
        /// </summary>
        private Point oldPoint;

        /// <summary>
        /// 蒙层
        /// </summary>
        Graphics mask;

        private Boolean isRightMousePressed = false;

        /// <summary>
        /// 画图类型  0随机线 1直线 2距形
        /// </summary>
        private int DrawingType = 0;

        /// <summary>
        /// 图片保存文件夹
        /// </summary>
        private String TempSaveFolder = "D:/QuickDrawScreenTemp/";

        public _DrawForm()
        {
            InitializeComponent();
            // 最大化
            this.WindowState = FormWindowState.Maximized;

        }


        /// <summary>
        /// 获取屏幕截图
        /// </summary>
        /// <returns></returns>
        private Bitmap GetScreenCapture()
        {
            Rectangle tScreenRect = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Bitmap tSrcBmp = new Bitmap(tScreenRect.Width, tScreenRect.Height); // 用于屏幕原始图片保存
            Graphics gp = Graphics.FromImage(tSrcBmp);
            gp.CopyFromScreen(0, 0, 0, 0, tScreenRect.Size);
            gp.DrawImage(tSrcBmp, 0, 0, tScreenRect, GraphicsUnit.Pixel);
            return tSrcBmp;
        }


        public void CaptureScreen()
        {

            // 不在任务栏显示
            this.ShowInTaskbar = false;
            this.TopMost = true;

            // 无边框
            this.FormBorderStyle = FormBorderStyle.None;

            // 获取屏幕
            Bitmap btp = GetScreenCapture();

            Image BackScreen = new Bitmap(btp, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

            // 在 BackScreen 上绘制蒙板
            mask = Graphics.FromImage(BackScreen);
            mask.FillRectangle(new SolidBrush(Color.FromArgb(0, 0, 0, 0)), 0, 0, BackScreen.Width, BackScreen.Height);
            this.BackgroundImage = BackScreen;

            startDrawing = true;

            this.Show();

        }

        private void Main_Load(object sender, EventArgs e)
        {
            this.DoubleBuffered = true;
            CaptureScreen();
        }


        private void Main_MouseMove(object sender, MouseEventArgs e)
        {

            if (!startDrawing)
            {
                return;
            }

            if (!isRightMousePressed)
            {
                return;
            }

            if (mask == null)
            {
                CaptureScreen();
            }


            //这里写的似乎写在后面一层了  显示不出来 关闭时能看得到
            if (DrawingType == 0)
            {
                mask.DrawLine(new Pen(Color.OrangeRed, 3), oldPoint, new Point(e.X, e.Y));
                oldPoint.X = e.X;
                oldPoint.Y = e.Y;

            }
            else if (DrawingType == 1)
            {
                //出来一个画笔,这只笔画出来的颜色是红的
                Pen p = new Pen(Brushes.Red);

                //创建两个点
                Point newPoint = new Point(e.X, e.Y);

                //将两个点连起来
                mask.DrawLine(p, oldPoint, newPoint);
            }
            else if (DrawingType == 2)
            {
                Pen p = new Pen(Brushes.Red);
                mask.DrawRectangle(p, new Rectangle(0, 0, 100, 200));
            }
            else
            {

                mask.DrawLine(new Pen(Color.OrangeRed, 3), oldPoint, new Point(e.X, e.Y));
                oldPoint.X = e.X;
                oldPoint.Y = e.Y;
            }

            this.Refresh();




        }

        private void Main_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.isRightMousePressed = false;
            }
        }

        private void Main_MouseDown(object sender, MouseEventArgs e)
        {

            if (startDrawing)
            {
                if (e.Button == MouseButtons.Left)
                {
                    this.isRightMousePressed = true;
                    oldPoint = new Point(e.X, e.Y);
                }
            }

            if (e.Button == MouseButtons.Right)
            {
                //不要给窗口设置菜单，不然右键也有效
                contextMenuStrip1.Show(e.X + this.Left, e.Y + this.Top);
            }

        }

        private void Main_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            BackToMainForm();
        }

        private void HideMenuItem_Click(object sender, EventArgs e)
        {
            startDrawing = false;
            isRightMousePressed = false;
            mask.Clear(Color.FromArgb(0, 0, 0, 0));
            mask = null;
            this.Hide();

            MainForm.MainFormInstance.Show();
        }

        private void RectMenuItem_Click(object sender, EventArgs e)
        {
            this.DrawingType = 1;
        }

        private void ArrowMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void SavePicMenuItem_Click(object sender, EventArgs e)
        {
            Bitmap bitmap = GetScreenCapture();
            if (Directory.Exists(TempSaveFolder) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(TempSaveFolder);
            }
            //MessageBox.Show("保存成功!");

        }

        private void ClearDrawMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("待开发...");
            return;
            startDrawing = false;
            isRightMousePressed = false;
            mask.Clear(Color.FromArgb(0, 0, 0, 0));
            mask = null;
            //this.Hide();
        }

        private void DrawForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
            {
                BackToMainForm();
            }
        }

        public void BackToMainForm()
        {
            startDrawing = false;
            isRightMousePressed = false;
            mask.Clear(Color.FromArgb(0, 0, 0, 0));
            mask = null;
            this.Hide();

            MainForm.MainFormInstance.Show();

        }

    }
}
